package com.isoft.service.flightUpdate.impl;

import com.isoft.bean.ResponseData;
import com.isoft.dao.flightUpdate.FlightUpdateInfoDao;
import com.isoft.dao.flightUpdate.FlightVoDao;
import com.isoft.entity.flightUpdate.FlightUpdateInfo;
import com.isoft.entity.flightUpdate.FlightUpdateVo;
import com.isoft.service.email.SendEmailService;
import com.isoft.service.email.ThreadService;
import com.isoft.service.flightUpdate.FlightUpdateInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LiuGaoMing
 * @version 1.0
 * @description:
 * @date 2024/6/30 16:26
 */
@Service
public class FlightUpdateInfoServiceImpl implements FlightUpdateInfoService {
    @Autowired
    private FlightUpdateInfoDao flightUpdateInfoDao;

    @Autowired
    private FlightVoDao flightVoDao;
    @Autowired
    private ThreadService threadService;

    @Autowired
    private SendEmailService sendEmailService;


//  分页查询所有
    @Override
    public List<FlightUpdateVo> getFlightUpdateInfo(int currentPage, int pageSize) {
        List<FlightUpdateVo> list = new ArrayList<>();
        Integer lastProcessedMetaId = null;
//      计算偏移量
        int offset = (currentPage - 1) * pageSize;
//        1，查到所有的数据
        List<FlightUpdateInfo> all = flightUpdateInfoDao.getFlightUpdateInfoPaged(offset, pageSize);
        // 2，获取总记录数
        Long total = flightUpdateInfoDao.getTotalFlightUpdateInfoCount();
//        3，看META_ID的数据有几条
        for (FlightUpdateInfo info : all) {
            if (lastProcessedMetaId != null && lastProcessedMetaId.equals(info.getMETAID())) {
                continue; // 如果当前META_ID与上一个处理过的相同，则跳过本次循环
            }
            FlightUpdateVo flightUpdateVo = new FlightUpdateVo(); // 确保每次循环都是一个新的FlightUpdateVo实例
            Integer[] apnos = flightUpdateInfoDao.getAPNOByMETAID(info.getMETAID());
            // 2. 根据APNO的数量设置APNO1, APNO2, APNO3
            switch (apnos.length) {
                case 2:
                    flightUpdateVo.setAPNO1(apnos[0]);
                    flightUpdateVo.setAPNO2(apnos[1]);
                    break;
                case 3:
                    flightUpdateVo.setAPNO1(apnos[0]);
                    flightUpdateVo.setAPNO2(apnos[1]);
                    flightUpdateVo.setAPNO3(apnos[2]);
                    break;
            }
            // 4. 通过APNO获取对应的APCD并设置
            if(flightUpdateVo.getAPNO3() == null){
                flightUpdateVo.setAPCD1(flightUpdateInfoDao.getAPCDByMETAIDAndAPNO(info.getMETAID(),flightUpdateVo.getAPNO1()));
                flightUpdateVo.setAPCD2(flightUpdateInfoDao.getAPCDByMETAIDAndAPNO(info.getMETAID(),flightUpdateVo.getAPNO2()));
            }else {
                flightUpdateVo.setAPCD1(flightUpdateInfoDao.getAPCDByMETAIDAndAPNO(info.getMETAID(),flightUpdateVo.getAPNO1()));
                flightUpdateVo.setAPCD3(flightUpdateInfoDao.getAPCDByMETAIDAndAPNO(info.getMETAID(),flightUpdateVo.getAPNO2()));
                flightUpdateVo.setAPCD2(flightUpdateInfoDao.getAPCDByMETAIDAndAPNO(info.getMETAID(),flightUpdateVo.getAPNO3()));
            }
            flightUpdateVo.setAPAT(info.getAPAT());
            flightUpdateVo.setMETAID(info.getMETAID());
            flightUpdateVo.setSTATU(info.getSTATU());
            // 5. 将构建好的FlightUpdateVo添加到结果列表
            list.add(flightUpdateVo);
            lastProcessedMetaId = info.getMETAID(); // 更新最后处理的META_ID
        }
//        获取到所有list数据

        return list;
    }
    //    获取全部记录数的分页和total总数
    public ResponseData<List<FlightUpdateVo>> getFlightUpdateInfoWithTotal(int currentPage, int pageSize) {
        List<FlightUpdateVo> data = getFlightUpdateInfo(currentPage, pageSize);
        Long total = flightUpdateInfoDao.getTotalFlightUpdateInfoCount();
        return ResponseData.success(data, total);
    }

//  精准查询根据metaid
    @Override
    public List<FlightUpdateVo> getFlightUpdateInfoByMETAID(Integer META_ID) {
        List<FlightUpdateVo> list = new ArrayList<>();
        FlightUpdateVo flightUpdateVo = new FlightUpdateVo();
            Integer lastProcessedMetaId = null;
            List<FlightUpdateInfo> flightUpdateInfo = flightUpdateInfoDao.getFlightUpdateInfoByMETAID(META_ID);
            for (FlightUpdateInfo info : flightUpdateInfo) {
                if (lastProcessedMetaId != null && lastProcessedMetaId.equals(info.getMETAID())) {
                    continue;
                }
                Integer[] apnos = flightUpdateInfoDao.getAPNOByMETAID(info.getMETAID());
                switch (apnos.length) {
                    case 2:
                        flightUpdateVo.setAPNO1(apnos[0]);
                        flightUpdateVo.setAPNO2(apnos[1]);
                        break;
                    case 3:
                        flightUpdateVo.setAPNO1(apnos[0]);
                        flightUpdateVo.setAPNO2(apnos[1]);
                        flightUpdateVo.setAPNO3(apnos[2]);
                        break;
                }
                if(flightUpdateVo.getAPNO3() == null){
                    flightUpdateVo.setAPCD1(flightUpdateInfoDao.getAPCDByMETAIDAndAPNO(info.getMETAID(),flightUpdateVo.getAPNO1()));
                    flightUpdateVo.setAPCD2(flightUpdateInfoDao.getAPCDByMETAIDAndAPNO(info.getMETAID(),flightUpdateVo.getAPNO2()));
                }else {
                    flightUpdateVo.setAPCD1(flightUpdateInfoDao.getAPCDByMETAIDAndAPNO(info.getMETAID(),flightUpdateVo.getAPNO1()));
                    flightUpdateVo.setAPCD3(flightUpdateInfoDao.getAPCDByMETAIDAndAPNO(info.getMETAID(),flightUpdateVo.getAPNO2()));
                    flightUpdateVo.setAPCD2(flightUpdateInfoDao.getAPCDByMETAIDAndAPNO(info.getMETAID(),flightUpdateVo.getAPNO3()));
                }
                flightUpdateVo.setAPAT(info.getAPAT());
                flightUpdateVo.setMETAID(info.getMETAID());
                lastProcessedMetaId = info.getMETAID(); // 更新最后处理的META_ID
                list.add(flightUpdateVo);
            }
        return list;
        }

//        航班航线变更
    @Override
    public Boolean updateFlightUpdateInfoByMETAID(FlightUpdateVo flightUpdateVo) {

        if (!flightUpdateVo.getSTATU().equals("待审核")) {

            String email = flightUpdateVo.getEmail();
            threadService.sendSimpleMail(
                    email,
                    "航班航线变更通知",
                    "尊敬的用户，您好！您的航班航线变更申请已处理，请及时关注！");
        }
        Integer metaid = flightUpdateVo.getMETAID();
        List<FlightUpdateInfo> flightUpdateInfo = flightUpdateInfoDao.getFlightUpdateInfoByMETAID(metaid);
        FlightUpdateVo insertFlightVo = new FlightUpdateVo();
        if(flightUpdateInfo.size() == 0){
            return false;
        }
//        给航班航线变更历史的值插入到数据库中
        insertFlightVo.setMETAID(flightUpdateVo.getMETAID());
        insertFlightVo.setAPAT(flightUpdateVo.getAPAT());
        insertFlightVo.setAPNO1(flightUpdateVo.getAPNO1());
        insertFlightVo.setAPNO2(flightUpdateVo.getAPNO2());
        insertFlightVo.setAPNO3(flightUpdateVo.getAPNO3());
//        设置旧值
        for (FlightUpdateInfo updateInfo : flightUpdateInfo) {
            if(updateInfo.getAPNO() == 1){
                insertFlightVo.setAPCD1(updateInfo.getAPCD());
            }
            if(updateInfo.getAPNO() == 2){
                insertFlightVo.setAPCD2(updateInfo.getAPCD());
            }
            if(updateInfo.getAPNO() == 3){
                insertFlightVo.setAPCD3(updateInfo.getAPCD());
            }
        }
//        如果是进行变更，APCD1，APCD2，APCD3就从DFME_AIRL_HISROTY中查找
//        通过STATU和META_ID查找DFME_AIRL_HISROTY中待审核的APCD1，APCD2，APCD3赋值给vo
        List<FlightUpdateVo> insertResultVo =  flightVoDao.getAPCD("待审核",flightUpdateVo.getMETAID());
        if(!flightUpdateVo.getSTATU().equals("待审核")){
//            获取到listinsertResultVo里头最后一个insertResultVo
            for (FlightUpdateVo updateVo : insertResultVo) {
            insertFlightVo.setAPCD1(updateVo.getAPCD1());
            insertFlightVo.setAPCD2(updateVo.getAPCD2());
            insertFlightVo.setAPCD3(updateVo.getAPCD3());
            }
        }
//        设置新值
        insertFlightVo.setNAPCD1(flightUpdateVo.getAPCD1());
        insertFlightVo.setNAPCD2(flightUpdateVo.getAPCD2());
        insertFlightVo.setNAPCD3(flightUpdateVo.getAPCD3());
        insertFlightVo.setSTATU(flightUpdateVo.getSTATU());
//        插入数据库中
            flightVoDao.insertFlightVo(insertFlightVo);

//           始发站
            FlightUpdateInfo startInfo = new FlightUpdateInfo();
            startInfo.setMETAID(flightUpdateVo.getMETAID());
            startInfo.setAPAT(flightUpdateVo.getAPAT());
            startInfo.setAPNO(flightUpdateVo.getAPNO1());
            startInfo.setAPCD(flightUpdateVo.getAPCD1());
            startInfo.setSTATU(flightUpdateVo.getSTATU());
//            修改始发站
            flightUpdateInfoDao.updateStart(startInfo);
//          判断flightUpdateVo.getAPCD3是否为空
            if(!flightUpdateVo.getAPCD3().equals("无" ) || flightUpdateVo.getAPCD3().equals("")){
                //            经停站
                FlightUpdateInfo endInfo = new FlightUpdateInfo();
                endInfo.setMETAID(flightUpdateVo.getMETAID());
                endInfo.setAPAT(flightUpdateVo.getAPAT());
                endInfo.setAPNO(flightUpdateVo.getAPNO3());
                endInfo.setAPCD(flightUpdateVo.getAPCD3());
                endInfo.setSTATU(flightUpdateVo.getSTATU());
//            修改原来的
                flightUpdateInfoDao.updateEnd(endInfo);
//                判断是否有第三条记录
                if(flightUpdateInfo.size()<3){
//            插入新的
                    FlightUpdateInfo newInfo = new FlightUpdateInfo();
                    newInfo.setMETAID(flightUpdateVo.getMETAID());
                    newInfo.setAPCD(flightUpdateVo.getAPCD2());
                    newInfo.setAPNO(flightUpdateVo.getAPNO3());
                    newInfo.setAPAT(flightUpdateVo.getAPAT());
                    newInfo.setSTATU(flightUpdateVo.getSTATU());
                    flightUpdateInfoDao.insertFlightUpdateInfo(newInfo);
                    return true;
                }else {
//            修改目的地
                    FlightUpdateInfo newInfo = new FlightUpdateInfo();
                    newInfo.setMETAID(flightUpdateVo.getMETAID());
                    newInfo.setAPCD(flightUpdateVo.getAPCD2());
                    newInfo.setAPNO(flightUpdateVo.getAPNO2());
                    newInfo.setAPAT(flightUpdateVo.getAPAT());
                    newInfo.setSTATU(flightUpdateVo.getSTATU());
                    flightUpdateInfoDao.updateENDT(newInfo);
                    return true;
                }
            }
            else {
                //            目的站
                FlightUpdateInfo endInfo = new FlightUpdateInfo();
                endInfo.setMETAID(flightUpdateVo.getMETAID());
                endInfo.setAPAT(flightUpdateVo.getAPAT());
                endInfo.setAPNO(flightUpdateVo.getAPNO2());
                endInfo.setAPCD(flightUpdateVo.getAPCD2());
                endInfo.setSTATU(flightUpdateVo.getSTATU());
//            修改原来的
                flightUpdateInfoDao.updateEnd(endInfo);
                return true;
            }

    }
}
